<!--
  * 实训室耗材申请
  *
  * @Author:    李韵妍
  * @Date:      2024-11-08 13:31:04
  * @Copyright  🐂🐎bloc
-->
<template>
  <a-modal
      title="审批"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" >

      <a-form-item label="申请人姓名"  name="applicant">
        <EmployeeSelect ref="employeeSelect" placeholder="请输入申请人姓名" width="100%"
                        v-model:value="form.actualName" disabled/>
      </a-form-item>
      <a-form-item label="申请班级"  name="classroom">
        <a-input style="width: 100%" v-model:value="form.classroom" placeholder="申请班级" disabled/>
      </a-form-item>
      <a-form-item label="借用设备名称"  name="equipmentName">
                  <a-input style="width: 100%" v-model:value="form.equipmentName" placeholder="借用设备的名称"  disabled/>
      </a-form-item>
      <a-form-item label="借用设备规格"  name="specificationModel">
        <a-input style="width: 100%" v-model:value="form.specificationModel" placeholder="借用设备的规格型号" disabled/>
      </a-form-item>
      <!--        <a-form-item label="借用设备的数量"  name="quantity">-->
      <!--          <a-input-number style="width: 100%" v-model:value="form.quantity" placeholder="借用设备的数量" />-->
      <!--        </a-form-item>-->
      <a-form-item label="详细的设备借用理由"  name="borrowReason">
        <a-textarea  style="width: 100%" v-model:value="form.borrowReason" placeholder="详细的设备借用理由" disabled />
      </a-form-item>
      <a-form-item label="借用日期"  name="borrowStartDate">
        <a-date-picker valueFormat="YYYY-MM-DD" v-model:value="form.borrowStartDate" style="width: 100%" placeholder="借用日期" disabled/>
      </a-form-item>
      <a-form-item label="归还日期"  name="borrowEndDate">
        <a-date-picker valueFormat="YYYY-MM-DD" v-model:value="form.borrowEndDate" style="width: 100%" placeholder="归还日期" disabled/>
      </a-form-item>
      <a-form-item label="备注信息"  name="remark">
        <a-textarea style="width: 100%" v-model:value="form.remark" placeholder="关于设备借用的其他备注信息"  disabled/>
      </a-form-item>
      <a-form-item label="审批"  name="approval">
        <a-radio-group v-model:value="form.approval"  name="approvalGroup">
          <a-radio :value="1">通过</a-radio>
          <a-radio :value="2">不通过</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="审批意见信息"  name="remark" v-if="form.approval == 2">
        <a-textarea v-if="role === roles.ADMIN" style="width: 100%" v-model:value="form.adminRemark" placeholder="管理员审批意见信息"  />
        <a-textarea v-else style="width: 100%" v-model:value="form.teacherRemark" placeholder="指导老师审批意见信息"  />
      </a-form-item>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">确认</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
import {reactive, ref, nextTick, onMounted, watch} from 'vue';
import _ from 'lodash';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { equipmentBorrowApplicationApi } from '/@/api/business/equipment-borrow-application/equipment-borrow-application-api';
import { smartSentry } from '/@/lib/smart-sentry';
import EmployeeSelect from '/@/components/system/employee-select/index.vue';
import {useUserStore} from "/@/store/modules/system/user.js";
import {positionApi} from "/@/api/system/position-api.js";


// ------------------------ 数据 ------------------------

const eqStatus = 0; // 设备状态 0:可用
const eqList = ref([]);
const userStore = useUserStore();
const roleName = ref([])
const role = ref('')
const roles = {
  TEACHER: '指导老师',
  ADMIN: '管理员',
}


// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);

  }
  // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
  // if (form.status && form.status.length > 0) {
  //   form.status = form.status.map((e) => e.valueCode);
  // }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const employeeSelect = ref();

const formDefault = {
  id:undefined, // 主键id
  borrowReason: undefined, //详细的设备借用理由
  applicant: undefined, //申请人姓名
  actualName: undefined, //实际姓名
  classroom: undefined, //申请班级
  borrowStartDate: undefined, //设备借用开始日期
  borrowEndDate: undefined, //设备借用结束日期
  equipmentName: undefined, //借用设备的名称
  specificationModel: undefined, //借用设备的规格型号
  quantity: undefined, //借用设备的数量
  borrowStatus: undefined, //设备当前的借出状态，1表示已借出，0表示未借出
  remark: undefined, //关于设备借用的其他备注信息
  guideTeacherSignature: undefined, //指导老师签字信息，可以是图片路径或其他标识
  adminSignature: undefined, //管理员签字信息，可以是图片路径或其他标识
  borrowDate: undefined, //设备实际借出日期
  returnConfirmation: undefined, //设备是否已确认归还，1表示已确认，0表示未确认
  returnDate: undefined, //设备归还日期
  returnStatus: undefined, //设备当前的归还状态，1表示已归还，0表示未归还
  equipmentList: [], // 实训室所有可用设备,
  approval:1,
  teacherRemark:undefined,
  adminRemark:undefined,
  role:undefined,
};


let form = reactive({ ...formDefault });

const rules = {

  borrowReason: [{ required: true, message: '详细的设备借用理由 必填' }],
  applicant: [{ required: true, message: '申请人姓名 必填' }],
  classroom: [{ required: true, message: '申请班级 必填' }],
  borrowStartDate: [{ required: true, message: '设备借用开始日期 必填' }],
  borrowEndDate: [{ required: true, message: '设备借用结束日期 必填' }],
  equipmentName: [{ required: true, message: '借用设备的名称 必填' }],
  specificationModel: [{ required: true, message: '借用设备的规格型号 必填' }],
  quantity: [{ required: true, message: '借用设备的数量 必填' }],
  borrowStatus: [{ required: true, message: '设备当前的借出状态，1表示已借出，0表示未借出 必填' }],
  remark: [{ required: true, message: '关于设备借用的其他备注信息 必填' }],
  guideTeacherSignature: [{ required: true, message: '指导老师签字信息，可以是图片路径或其他标识 必填' }],
  adminSignature: [{ required: true, message: '管理员签字信息，可以是图片路径或其他标识 必填' }],
  borrowDate: [{ required: true, message: '设备实际借出日期 必填' }],
  returnConfirmation: [{ required: true, message: '设备是否已确认归还，1表示已确认，0表示未确认 必填' }],
  returnDate: [{ required: true, message: '设备归还日期 必填' }],
  returnStatus: [{ required: true, message: '设备当前的归还状态，1表示已归还，0表示未归还 必填' }],
};



// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    let newForm = {
      adminRemark: form.adminRemark, // 管理员审批意见信息
      teacherRemark: form.teacherRemark, // 指导老师审批意见信息
      adminSignature: form.adminSignature, // 管理员签字信息，可以是图片路径或其他标识
      guideTeacherSignature: form.guideTeacherSignature, // 指导老师签字信息，可以是图片路径或其他标识
      id: form.id, // 主键id
      equipmentId: form.equipmentId, // 借用设备的id
      role:form.role
    }
    if(role.value === roles.ADMIN){
      newForm.adminSignature = form.approval
      newForm.role = roles.ADMIN
    }else{
      newForm.guideTeacherSignature = form.approval
      newForm.role = roles.TEACHER
    }


    await equipmentBorrowApplicationApi.approval(newForm);
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

// ------------------------ 实训室所有可用设备------------------------

async function loadEquipmentList(status) {
  try {
    const res = await equipmentBorrowApplicationApi.getAllEquipment(status);
    if (res && res.data) {
      eqList.value = res.data;

    }
  } catch (err) {
    smartSentry.captureError(err);
  }
}

function handleChange(value) {
  form.equipmentName = value;
}

watch(()=>form.equipmentName,(newValue,oldValue)=>{
  if (newValue) {
    const selectedEquipment = eqList.value.find((item) => item.equipmentName === newValue);
    if (selectedEquipment) {
      form.specificationModel = selectedEquipment.specificationModel;
    }
  }
})


function onChange(value){
  form.approval = value;
}

function roleFn(){
  roleName.value = userStore.getRoleList.map((_)=>{return _.roleName})
}

watch(()=>roleName.value,(newValue,oldValue)=>{
  if (newValue.includes(roles.ADMIN)) {
    role.value = roles.ADMIN
  } else if (newValue.includes(roles.TEACHER)) {
    role.value = roles.TEACHER
  } else {
    role.value = ''
  }
})


onMounted(()=> {
  loadEquipmentList(eqStatus)
  roleFn()

})

defineExpose({
  show,
});
</script>
